package leecode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname
 * @Description TODO
 * @Date 2021/7/27 16:54
 * @Created by Alberthch
 * 题目：最长递增子序列
 */
class Solution_300 {
    public static int lengthOfLIS(int[] nums) {
        int n = nums.length;
        List<Integer> d = new ArrayList<>();
        for(int num:nums) {

            int index = findIndex(d,num);
            if (index == d.size()) {
                d.add(num);
            } else {
                d.set(index,num);
            }
        }

        return d.size();
    }

    public static int findIndex(List<Integer> L,Integer target) {
        int n = L.size();
        if (n == 0 || target > L.get(n-1)) {
            return n;
        }

        int low = 0;
        int high = n-1;
        int mid;
        while (low < high) {
            mid = (high-low)/2 +low;
            if (L.get(mid) < target) {
                low = mid +1;
            } else {
                high = mid;
            }
        }
        return low;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{0,1,0,3,2,3};
        System.out.println(lengthOfLIS(arr));
    }
}
